package gui.ParkingLotWorker;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.SystemTypes;
import system.VCP;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;

import com.toedter.calendar.JDateChooser;

import control.LoginController;
/**
 * this class use to Save Parking Space Screen
 * @author G8
 *
 */
public class SaveParkingSpaceScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	public JButton btnSave;
	private JDateChooser textFieldDate;
	private JFormattedTextField textFieldTime;
	private JTextField textFieldClient;
	/**
	 *Save Parking Space Screen Constructor 
	 */
	public SaveParkingSpaceScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Save Parking Space", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(InitializeLotScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(185, 251, 97, 35);
		btnSave.addActionListener(this);
		add(btnSave);
		
		JLabel lblClient = new JLabel("Client:");
		lblClient.setBounds(126, 143, 72, 14);
		add(lblClient);
		
		JLabel lblSaveDate = new JLabel("Date:");
		lblSaveDate.setBounds(126, 168, 78, 14);
		add(lblSaveDate);
				
		JLabel lblPlease = new JLabel("Please select a client and a parking space to save...");
		lblPlease.setBounds(68, 96, 370, 14);
		add(lblPlease);
		
		JLabel lblSaveTime = new JLabel("Time:");
		lblSaveTime.setBounds(126, 194, 72, 14);
		add(lblSaveTime);
		
		textFieldDate = new JDateChooser();
		textFieldDate.setBounds(208, 165, 120, 20);
		add(textFieldDate);
		
		MaskFormatter formatTimeIn;
		try {
			formatTimeIn = new MaskFormatter("##:##");
			formatTimeIn.setValidCharacters("0123456789");
			textFieldTime = new JFormattedTextField(formatTimeIn);
			textFieldTime.setBounds(208, 191, 120, 20);
			add(textFieldTime);
			textFieldTime.setColumns(10);
			
			textFieldClient = new JTextField();
			textFieldClient.setBounds(208, 140, 120, 20);
			add(textFieldClient);
			textFieldClient.setColumns(10);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSave){}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
		}
		}
	}
}
